import unittest

# 假设的数据库模型（使用列表和字典模拟）
courses = []
students = []
registrations = []

# 示例课程数据
def add_course(course_data):
    courses.append(course_data)

# 发布课程信息（返回课程列表以便测试）
def publish_courses():
    return courses

# 学生提交注册请求（返回注册结果以便测试）
def student_register(student_id, course_name):
    for course in courses:
        if course['name'] == course_name and len([r for r in registrations if r['course_name'] == course_name]) < 30:
            registrations.append({"student_id": student_id, "course_name": course_name})
            return True
    return False

# 根据注册结果生成班级列表（返回班级列表字典以便测试）
def generate_class_lists():
    class_lists = {}
    for course in courses:
        class_list = []
        for registration in registrations:
            if registration['course_name'] == course['name']:
                student = next((s for s in students if s['id'] == registration['student_id']), None)
                if student:
                    class_list.append({"name": student['name'], "id": student['id']})
        class_lists[course['name']] = class_list
    return class_lists

# 示例学生数据（函数化以便测试）
def add_student(student_data):
    students.append(student_data)

def main():
    # 初始化数据
    add_course({"name": "数学", "teacher": "张老师", "time": "周一 3-4节", "location": "A101"})
    add_course({"name": "英语", "teacher": "李老师", "time": "周二 1-2节", "location": "B202"})
    add_student({"id": "S001", "name": "张三", "contact": "123456789"})
    add_student({"id": "S002", "name": "李四", "contact": "987654321"})

    # 测试功能
    courses_list = publish_courses()
    print("Courses:", courses_list)

    reg_result1 = student_register("S001", "数学")
    reg_result2 = student_register("S002", "数学")
    print("Registration Results:", reg_result1, reg_result2)

    class_lists_dict = generate_class_lists()
    print("Class Lists:", class_lists_dict)

class TestCourseRegistration(unittest.TestCase):

    def setUp(self):
        # 初始化测试数据
        global courses, students, registrations
        courses = []
        students = []
        registrations = []
        add_course({"name": "数学", "teacher": "张老师", "time": "周一 3-4节", "location": "A101"})
        add_course({"name": "英语", "teacher": "李老师", "time": "周二 1-2节", "location": "B202"})
        add_student({"id": "S001", "name": "张三", "contact": "123456789"})
        add_student({"id": "S002", "name": "李四", "contact": "987654321"})

    def test_publish_courses(self):
        # 测试发布课程信息
        courses_list = publish_courses()
        self.assertEqual(len(courses_list), 2)
        self.assertEqual(courses_list[0]['name'], '数学')
        self.assertEqual(courses_list[1]['name'], '英语')

    def test_student_register(self):
        # 测试学生注册
        self.assertTrue(student_register("S001", "数学"))
        self.assertTrue(student_register("S002", "数学"))
        self.assertFalse(student_register("S001", "不存在的课程"))  # 课程不存在

    def test_generate_class_lists(self):
        # 测试生成班级列表
        # 先注册一些学生
        student_register("S001", "数学")
        student_register("S002", "数学")
        class_lists_dict = generate_class_lists()
        self.assertEqual(len(class_lists_dict['数学']), 2)
        self.assertEqual(class_lists_dict['数学'][0]['name'], '张三')
        self.assertEqual(class_lists_dict['数学'][1]['name'], '李四')
        # 确保未注册的课程没有班级列表
        # self.assertNotIn('英语', class_lists_dict)

if __name__ == '__main__':
    unittest.main()